package com.tedu.parkinglot.payRecord.service.impl;

import com.tedu.parkinglot.base.constants.OrderConstant;
import com.tedu.parkinglot.order.mapper.OrderMapper;
import com.tedu.parkinglot.order.pojo.dto.OrderQuery;
import com.tedu.parkinglot.order.pojo.vo.OrderVO;
import com.tedu.parkinglot.parklot.mapper.ParklotMapper;
import com.tedu.parkinglot.parklot.pojo.dto.ParklotQuery;
import com.tedu.parkinglot.parklot.pojo.vo.ParkRecordVO;
import com.tedu.parkinglot.parklot.pojo.vo.ParklotVO;
import com.tedu.parkinglot.payRecord.mapper.PayRecordMapper;
import com.tedu.parkinglot.payRecord.pojo.dto.PayRecordQuery;
import com.tedu.parkinglot.payRecord.pojo.vo.ParkChargePayRecordVO;
import com.tedu.parkinglot.payRecord.pojo.vo.UserCardPayRecordVO;
import com.tedu.parkinglot.payRecord.service.PayRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class PayRecordServiceImpl implements PayRecordService {
    @Autowired
    private PayRecordMapper payRecordMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ParklotMapper parklotMapper;
    @Override
    public List<ParkChargePayRecordVO> searchParkChargRecord(PayRecordQuery payRecordQuery) {
        List<ParkChargePayRecordVO> list= payRecordMapper.searchParkChargRecord(payRecordQuery);
        log.debug("searchParkChargRecord payRecordQuery服务:{}",list);
        for (int i = 0; i < list.size(); i++)
         {
             ParkChargePayRecordVO parkChargePayRecordVO = list.get(i);
            OrderQuery orderQuery = new OrderQuery();
            orderQuery.setId(parkChargePayRecordVO.getOrderId());
            orderQuery.setOrderType(OrderConstant.ORDER_TYPE_PARKCHARGE);
            OrderVO orderVO = orderMapper.selectOrderByIdAndType(orderQuery);
            if (orderVO==null){
                list.remove(parkChargePayRecordVO);
                i--;
                continue;
            }
            ParkRecordVO parkRecordVO = parklotMapper.selectRecordById(orderVO.getParkRecordId());//bug
            if (parkRecordVO==null){
                list.remove(parkChargePayRecordVO);
                i--;
                continue;
            }
            parkChargePayRecordVO.setParkTime(parkRecordVO.getUserTime());
            //将停车时长分钟转换为小时加分钟格式
            parkChargePayRecordVO.setDuration(parkRecordVO.getUserTime()/60+"小时"+parkRecordVO.getUserTime()%60+"分钟");
        }
        log.debug("list:{}",list);
        return list==null? Collections.emptyList():list;
    }

    @Override
    public List<UserCardPayRecordVO> searchUserCardRecord(PayRecordQuery payRecordQuery) {
        log.debug("searchUserCardRecord payRecordQuery服务:{}",payRecordQuery);
        payRecordQuery.setOrderType(OrderConstant.ORDER_TYPE_PARKCARD);
        List<UserCardPayRecordVO>  list=payRecordMapper.selectUserCardRecord(payRecordQuery);
        return list;
    }
}
